﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace CRM.Models.Email
{
    [Index(nameof(Source))]
    [Index(nameof(ReceiverEmail))]
    [Index(nameof(SendingType))]
    [Index(nameof(EmailPlatform))]
    [Index(nameof(OemId), nameof(CompanyId), nameof(GroupId), nameof(UserId))]
    public partial class EmailSendHistory : BaseModel
    {
        [Comment("id")]
        public int Id { get; set; }
        
        [Comment("UserId")]
        public int UserId { get; set; }
        
        [Comment("GroupId")]
        public int GroupId { get; set; }
        
        [Comment("CompanyId")]
        public int CompanyId { get; set; }
        
        [Comment("OemId")]
        public int OemId { get; set; }

        [Comment("客户名称")]
        [Column(TypeName = "varchar(30)")]
        public string Name { get; set; }

        [Comment("客户接收邮箱")]
        [Column(TypeName = "varchar(50)")]
        public string ReceiverEmail { get; set; }

        [Comment("发送类型")]
        [Column(TypeName = "tinyint")]
        public SendingTypeEnum SendingType { get; set; }        
        
        [Comment("发送平台"), Column(TypeName = "tinyint")]
        public EmailPlatform EmailPlatform { get; set; }

        [Comment("发送人邮箱")]
        [Column(TypeName = "varchar(50)")]
        public string SenderEmail { get; set; }

        [Comment("状态")]
        public bool State { get; set; }

        [Comment("营销任务来源")]
        public int Source { get; set; }
        
        [Comment("营销任务名称")]
        [Column(TypeName = "varchar(50)")]
        public string MarketingTaskName { get; set; }

        [Comment("MessageUUID")]
        public string MessageUUID { get; set; }
        
        [Comment("MessageID")]
        public long MessageID { get; set; }

        [NotMapped] public string MessageIDString => MessageID.ToString();
        
        [Comment("MessageHref")]
        public string MessageHref { get; set; } 
        
        [Comment("Details")]
        [Column(TypeName = "bit(8)")]
        public long Details { get; set; }

        public enum SendingTypeEnum
        {
            [Description("免费")]
            Free = 1,
            [Description("服务器")]
            Server
        }
        
        public bool IsOpen => Convert.ToBoolean(Details & (int)DetailsEnum.Open);
        public bool IsSent => Convert.ToBoolean(Details & (int)DetailsEnum.Sent);
        public bool IsBounce => Convert.ToBoolean(Details & (int)DetailsEnum.Bounce);
        public bool IsUnsub => Convert.ToBoolean(Details & (int)DetailsEnum.Unsub);
        public bool IsClick => Convert.ToBoolean(Details & (int)DetailsEnum.Click);
        public bool IsSpam => Convert.ToBoolean(Details & (int)DetailsEnum.Spam);
        public bool IsBlocked => Convert.ToBoolean(Details & (int)DetailsEnum.Blocked);
    }
    
    /// <summary>
    /// 事件类型
    /// open 打开邮件
    /// sent 发送成功
    /// bounce 邮件被退回
    /// unsub 取消订阅
    /// click 点击了链接
    /// spam 邮件被标记为垃圾邮件
    /// blocked 邮箱被拉黑
    /// </summary>
    public enum DetailsEnum
    {
        Open = 1,
        Sent = 2,
        Bounce = 4,
        Unsub = 8,
        Click = 16,
        Spam = 32,
        Blocked = 64
    }
}